home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / comm / net / amitcp2_x_gcc.lha / RCS.RCSfiles / serveraccept.c,v < prev   
Text File  |  1994-03-20  |  4KB  |  185 lines

  1. head    1.1;
  2. access;
  3. symbols;
  4. locks
  5.     jasegler:1.1; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.1
  10. date    94.03.20.17.11.24;    author jasegler;    state Exp;
  11. branches;
  12. next    ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19. 1.1
  20. log
  21. @Initial revision
  22. @
  23. text
  24. @char RCS_ID_SERVERACCEPT_C[] = "$Id: serveraccept.c,v 1.1 1993/06/03 23:27:19 ppessi Exp $";
  25. /*
  26.  * Author: ppessi <Pekka.Pessi@@hut.fi>
  27.  *
  28.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@@hut.fi>
  29.  *                  Helsinki University of Technology, Finland.
  30.  *                  All rights reserved.
  31.  *
  32.  * Created      : Tue May 25 19:30:13 1993 ppessi
  33.  * Last modified: Fri Jun  4 02:26:40 1993 ppessi
  34.  *
  35.  * $Log: serveraccept.c,v $
  36.  * Revision 1.1  1993/06/03  23:27:19  ppessi
  37.  * Cosmetic changes for version 1.0
  38.  *
  39.  */
  40.  
  41. /****** inetdlib.doc/serveraccept *******************************************
  42.  
  43.  *  NAME
  44.  *      serveraccept - Accept a server connection on named port
  45.  *
  46.  *  SYNOPSIS
  47.  *      socket = serveraccept(name, peer);
  48.  *
  49.  *      long serveraccept(char *, struct sockaddr_in *);
  50.  *
  51.  *  DESCRIPTION
  52.  *      The serveraccept() library call binds a socket to the named Internet
  53.  *      TCP port. Then it listens the socket and accepts the connection to
  54.  *      the port. The peer's socket address is returned in sockaddr pointed
  55.  *      by sockaddr argument.
  56.  *
  57.  *      The port name is resolved by getservbyname() call. A numeric value
  58.  *      for port name is also accepted.
  59.  *
  60.  *      This module is meant for daemon developing.
  61.  *
  62.  *  INPUTS
  63.  *      name     - port name or numeric string.
  64.  *      peer     - pointer to struct sockaddr_in
  65.  *
  66.  *  RESULT
  67.  *       socket - positive socket id for success or -1 for failure.
  68.  *
  69.  *       peer   - sockaddr_in structure containing peer's internet address.
  70.  *                Note that on error, the structure containing peer address
  71.  *                is not necessarily updated.
  72.  *
  73.  *  AUTHOR
  74.  *      Pekka Pessi, the AmiTCP/IP Group, <amitcp-group@@hut.fi>,
  75.  *      Helsinki University of Technology, Finland.
  76.  *
  77.  *  SEE ALSO
  78.  *       bsdsocket/accept, bsdsocket/getservbyname
  79.  *
  80.  *****************************************************************************
  81.  *
  82.  */
  83.  
  84. #ifdef AMIGA
  85. #if __SASC
  86. #include <proto/socket.h>
  87. #include <proto/dos.h>
  88. #include <clib/exec_protos.h>
  89. #include <pragmas/exec_sysbase_pragmas.h>
  90. #elif __GNUC__
  91. #include <clib/socket_protos.h>
  92. #include <clib/exec_protos.h>
  93. #elif _DCC
  94. #include <clib/socket_protos.h>
  95. #include <clib/exec_protos.h>
  96. #endif
  97. #endif /* AMIGA */
  98.  
  99. #include <errno.h>
  100. #include <netdb.h>
  101.  
  102. #include <sys/param.h>
  103. #include <sys/socket.h>
  104. #include <sys/ioctl.h>
  105. #include <netinet/in.h>
  106.  
  107. #include <signal.h>
  108.  
  109. #include <dos/dos.h>
  110. #include <exec/execbase.h>
  111. #include <dos/var.h>
  112.  
  113. #include <stdlib.h>
  114.  
  115. /*
  116.  * serveraccept:
  117.  *      Accept a server socket from the named port
  118.  */
  119. long
  120. serveraccept (char *pname, struct sockaddr_in *ha)
  121. {
  122.   struct sockaddr_in sin;
  123.   long ha_len = sizeof (*ha);
  124.   int s, sa;
  125.   int port;
  126.   struct servent *sp;
  127.   long on = 1;
  128.  
  129.   /* Create address corresponding our service */
  130.   bzero ((caddr_t) & sin, sizeof (sin));
  131. #ifdef BSD4_4
  132.   sin.sin_len = sizeof (struct sockaddr_in);
  133. #endif
  134.   sin.sin_family = AF_INET;
  135.   /* A port must be in the range 1 - 65535 */
  136.   if ((port = strtoul (pname, NULL, 0)) && port < 65536)
  137.     sin.sin_port = port;
  138.   else if (sp = getservbyname (pname, "tcp"))
  139.     sin.sin_port = sp->s_port;
  140.   else
  141.     {
  142.       return -1;
  143.     }
  144.   sin.sin_addr.s_addr = INADDR_ANY;
  145.  
  146.   if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0)
  147.     {
  148.       PrintNetFault (Errno (), "socket");
  149.       return -1;
  150.     }
  151.  
  152.   /* Reuse this port */
  153.   if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, (char *) &on, sizeof (on)) < 0)
  154.     {
  155.       PrintNetFault (Errno (), "setsockopt");
  156.       sa = -1;
  157.       goto Return;
  158.     }
  159.  
  160.   /* Bind it to socket */
  161.   if (bind (s, (struct sockaddr *) &sin, sizeof (sin)) < 0)
  162.     {
  163.       PrintNetFault (Errno (), "bind");
  164.       sa = -1;
  165.       goto Return;
  166.     }
  167.  
  168.   if (listen (s, 1) < 0)
  169.     {
  170.       PrintNetFault (Errno (), "listen");
  171.       sa = -1;
  172.       goto Return;
  173.     }
  174.  
  175.   if ((sa = accept (s, (struct sockaddr *) ha, &ha_len)) < 0)
  176.     {
  177.       PrintNetFault (Errno (), "accept");
  178.     }
  179.  
  180. Return:
  181.   CloseSocket (s);
  182.   return sa;
  183. }
  184. @
  185.